home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / memory / memman2b / bigarray.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-19  |  4.1 KB  |  162 lines

  1. extern "C" {
  2.     #include "memory.h"
  3. };
  4. #include "bigarray.hpp"
  5.  
  6. // Constructor for BigArray
  7. BigArray::BigArray( long          NElements,
  8.                     unsigned      ElementSize,
  9.                     char         *AllocStrategy) {
  10.  
  11.     // Work out the size of the Array
  12.     ArraySize =    NElements * ElementSize;
  13.     if (!ArraySize) {status = 0;return;}
  14.  
  15.     // Decide on a sensible line length. A 2K buffer gives reasonable
  16.     // Performance, but the line must be a multiple of the ElementSize
  17.  
  18.     if (ElementSize > 2048) Width = ElementSize;
  19.     else Width = (2048/ElementSize) * ElementSize;
  20.  
  21.     // So how many of those lines will we need?
  22.     Lines = ArraySize/Width + (ArraySize%Width ? 1:0);
  23.  
  24.     // Now attempt to allocate a buffer
  25.     setXMstrat(AllocStrategy);
  26.     Handle = xalloc(Width,Lines);
  27.     if (!Handle) {
  28.         status = 0;
  29.         return;
  30.     }
  31.  
  32.     // Ok, we have a buffer. Assign the other stuff, get the first line, etc
  33.     offset = 0;
  34.     Item   = 0;
  35.     Items  = NElements;
  36.     Line   = 0;
  37.     step   = ElementSize;
  38.     status = 1;
  39.     LineBuf= xget(0,Handle);
  40. }
  41.  
  42. /***    Destructor Simply Frees The Buffer
  43. ***/
  44. BigArray::~BigArray() { xfree(Handle);}
  45.  
  46. /***    Type of memory allocated
  47. ***/
  48. char *BigArray::Type(void) {status=1;return mem_type(Handle);}
  49.  
  50. /***    Error test
  51. ***/
  52. int BigArray::OK(void) {return status;}
  53. int BigArray::operator!(void) {return !status;}
  54.  
  55. /***    '*' (indirection) operator overload
  56.         Return a pointer to the current element as a byte *
  57. ***/
  58. byte *BigArray::operator*(void) {
  59.     status=1;
  60.     offset = Item * step;   // Reset the offset pointer always
  61.     return LineBuf + ((long)Item*step - (long)Line*Width);
  62. }
  63.  
  64. /***    '[]' Punctuator Overload
  65.         Reposition array to given item, set status and return a pointer to it
  66. ***/
  67. byte *BigArray::operator[](unsigned long i) {
  68.  
  69.     offset = Item * step;   // Reset the offset pointer always
  70.  
  71.     if (i>=Items) {status=0; return NULL;}
  72.  
  73.     Item   = i;
  74.     offset = i * step;
  75.     Line   = offset/Width;
  76.     LineBuf=xget(Line,Handle);
  77.     status = (LineBuf?1:0);
  78.     return LineBuf + ((long)Item*step - (long)Line*Width);
  79. }
  80.  
  81. /***    Increment/Decrement operator overloads
  82. ***/
  83. int BigArray::operator++(void)
  84. {
  85.     offset = Item * step;   // Reset the offset pointer always
  86.  
  87.     if (Item+1>=Items) {status=0; return 0;}
  88.  
  89.     Item++;
  90.     offset = Item * step;
  91.     Line   = offset/Width;
  92.     LineBuf= xget(Line,Handle); // Will come right back if it's the same line
  93.     status = (LineBuf?1:0);
  94.  
  95.     return status;
  96. }
  97.  
  98. int BigArray::operator--(void)
  99. {
  100.     offset = Item * step;   // Reset the offset pointer always
  101.  
  102.     if (!Item) {status=0; return 0;}
  103.  
  104.     Item--;
  105.     offset = Item * step;
  106.     Line   = offset/Width;
  107.     LineBuf= xget(Line,Handle); // Will come right back if it's the same line
  108.     status = (LineBuf?1:0);
  109.  
  110.     return status;
  111. }
  112.  
  113. /***    >> overload:    Extract this byte and increment offset
  114. ***/
  115. int BigArray::operator>>(byte &recipient)
  116. {
  117.     if (offset >= ArraySize) {status=0;return -1;}
  118.  
  119.     recipient = LineBuf[offset - ((long)Line * Width)];
  120.  
  121.     offset++;
  122.     status=1;
  123.  
  124.     if (offset >= ArraySize) return 0; // Don't get next line if we're
  125.                                             // At the end
  126.  
  127.     // Maybe move onto next line
  128.     if (offset/step != Item) {
  129.         Item   = offset/step;
  130.         Line   = offset/Width;
  131.         LineBuf= xget(Line,Handle);
  132.         status = (LineBuf?1:0);
  133.     }
  134.  
  135.     return 0;
  136. }
  137.  
  138. /***    << overload:    Store this byte and increment offset
  139. ***/
  140. int BigArray::operator<<(byte newval)
  141. {
  142.     if (offset >= ArraySize) {status=0;return -1;}
  143.  
  144.     LineBuf[offset - ((long)Line * Width)] = newval;
  145.  
  146.     offset++;
  147.     status=1;
  148.  
  149.     if (offset >= ArraySize) return status; // Don't get next line if we're
  150.                                             // At the end
  151.  
  152.     // Maybe move onto next line
  153.     if (offset/step != Item) {
  154.         Item   = offset/step;
  155.         Line   = offset/Width;
  156.         LineBuf= xget(Line,Handle);
  157.         status = (LineBuf?1:0);
  158.     }
  159.  
  160.     return status;
  161. }
  162.